﻿package core.drawing
{
    import core.enum.*;
    import core.image.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    final public class GradientTool extends Tool
    {
        var pt:Point;
        var pr:Point;
        var po:Point;

        public function GradientTool(param1:Tuyitu)
        {
            super(param1, ToolType.Gradient, false);
            return;
        }// end function

        override public function Reset() : void
        {
            type.attitude.locked = Boolean(ed.canvas.layer.type != LayerType.Bitmap);
            if (AppState.OnCanvas)
            {
                ed.cursor.Set(type);
            }
            ed.options.SetTool(type);
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            if (!type.attitude.locked)
            {
                this.UpdateReferences();
                history.Prepare(Action.GradientTool);
                ed.cursor.freeze = true;
                ed.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false, 1, true);
                ed.addEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false, 1, true);
                this.pt = canvas.GetMousePosition();
                this.pr = canvas.GetRealMousePosition();
                Misc.DrawCross(canvas.coating.bitmapData, this.pr);
            }
            return;
        }// end function

        override public function MouseDrag(event:MouseEvent) : void
        {
            canvas.coating.Clear();
            this.po = canvas.GetRealMousePosition();
            Misc.DrawCross(canvas.coating.bitmapData, this.pr);
            Misc.DrawLine(canvas.coating.bitmapData, this.pr, this.po);
            canvas.coating.Redraw();
            return;
        }// end function

        override public function MouseUp(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Matrix = null;
            var _loc_4:Sprite = null;
            var _loc_5:Sprite = null;
            var _loc_6:BitmapData = null;
            var _loc_7:Rectangle = null;
            var _loc_8:uint = 0;
            var _loc_9:int = 0;
            ed.cursor.freeze = false;
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            canvas.coating.Clear();
            canvas.coating.Redraw();
            this.po = canvas.GetMousePosition();
            if (this.pt.x != this.po.x || this.pt.y != this.po.y)
            {
                _loc_3 = new Matrix();
                if (options.type == "linear")
                {
                    _loc_7 = Misc.CalculateRectangle(this.pt, this.po);
                    _loc_2 = _loc_7.width < _loc_7.height ? (_loc_7.height) : (_loc_7.width);
                    _loc_3.createGradientBox(_loc_2, _loc_2, Math.atan2(this.po.y - this.pt.y, this.po.x - this.pt.x), _loc_7.x, _loc_7.y);
                }
                else
                {
                    _loc_2 = Point.distance(this.pt, this.po);
                    _loc_3.createGradientBox(_loc_2 * 2, _loc_2 * 2, 0, this.pt.x - _loc_2, this.pt.y - _loc_2);
                }
                if (layer.editMask)
                {
                    _loc_8 = ed.toolbar.GetColor(1, true);
                    _loc_9 = 0;
                    while (_loc_9 < options.colors.length)
                    {
                        
                        options.colors[_loc_9] = _loc_8;
                        _loc_9++;
                    }
                }
                _loc_4 = new Sprite();
                _loc_4.graphics.beginGradientFill(options.type, options.colors, options.alphas, options.ratios, _loc_3, options.method);
                _loc_4.graphics.drawRect(0, 0, canvas.dw, canvas.dh);
                _loc_4.graphics.endFill();
                _loc_4.alpha = options.opacity;
                _loc_4.blendMode = options.blendmode;
                _loc_5 = new Sprite();
                _loc_5.addChild(_loc_4);
                _loc_6 = layer.SetBitmapEditMode(1, new Rectangle(0, 0, canvas.dw, canvas.dh));
                if (_loc_4.blendMode != "normal" && layer.activeBitmap != null)
                {
                    _loc_6.copyPixels(layer.activeBitmap, layer.activeBitmap.rect, new Point(layer.x, layer.y));
                }
                _loc_6.draw(_loc_5);
                Misc.ApplySelectionMask(_loc_6, selection, new Point(0, 0));
                layer.RemoveBitmapEditMode();
                canvas.UpdateAfterEdit();
                history.Commit();
            }
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

    }
}
